package com.liuwei.spring.cloud.junit.leetcode.array;

import lombok.extern.slf4j.Slf4j;

/**
 * https://leetcode.cn/problems/jump-game/?envType=study-plan-v2&envId=top-interview-150
 */
@Slf4j
public class JumpGame {

    public static void main(String[] args) {
        JumpGame instance = new JumpGame();
        //int[] nums = {2, 3, 1, 1, 4};
        //int[] nums = {3,2,1,0,4};
        int[] nums = {1,2,3};
        boolean jumped = instance.canJump(nums);
        System.out.println("jumped:"+jumped);
    }

    public boolean canJump(int[] nums) {
        int i = 0;
        int maxDistance = 0;
        while ((i < nums.length && i<= maxDistance)||i==0) {
            int temp = nums[i];
            int distance = i+temp;
            if (distance >= maxDistance) {
                maxDistance = distance;
            }
            log.info("i:{} maxDistance:{}",i,maxDistance);
            i++;
        }
        return maxDistance+1 >= nums.length;
    }
}
